package org.edu.controller;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.ListDataModel;

import org.edu.model.Address;
import org.edu.model.Teacher;
import org.edu.model.User;
import org.edu.service.TeacherService;
import org.edu.service.UserService;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.SelectableDataModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@SessionScoped
@Controller
public class TeacherController extends ListDataModel<Teacher> implements Serializable, SelectableDataModel<Teacher> {
	
	private static final long serialVersionUID = 1L;
	@Autowired
	private transient TeacherService teacherService;
	@Autowired
	private transient UserService userService;
	private Teacher teacher;
	private User user;
	private Address address;
	private List<User> userList;
	private List<Teacher> teacherList;
	private List<Teacher> filterTeacher;

	public TeacherController() {
		teacher = new Teacher();
		user = new User();
		address = new Address();
	}
	
	@PostConstruct
	public void init(){
		userList = userService.users();
		teacherList = teacherService.teachers();
	}
	
	public String preparedAddTeacher(){
		teacher = new Teacher();
		user = new User();
		address = new Address();
		return "newteacher";
	}

	@Override
	public Object getRowKey(Teacher object) {
		 return teacher.getUserTeacher().getUser_number();
	}
	
	@Override
	public Teacher getRowData(String rowKey) {
		for(Teacher teacher: teacherList){
			if(teacher.getUserTeacher().getUser_number().equals(rowKey))  
                return teacher;  
		}
		return null;
	}
	
	public void onRowSelect(SelectEvent event) {  
        FacesMessage msg = new FacesMessage("Teacher Selected", ((Teacher) event.getObject()).getUserTeacher().getUser_number());;  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
 }

	public Teacher getTeacher() {
		if(teacher == null){
			teacher = new Teacher();
		}
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
	
	public User getUser() {
		if(user == null){
			user = new User();
		}
		return user;
	}

	public void setUser(User user) {
		if(address == null){
			address = new Address();
		}
		this.user = user;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	public List<Teacher> getTeacherList() {
		return teacherList;
	}

	public List<Teacher> getFilterTeacher() {
		return filterTeacher;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setFilterTeacher(List<Teacher> filterTeacher) {
		this.filterTeacher = filterTeacher;
	}
	
}
